package com.ydl.service;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ydl.common.util.ResultVo;
import com.ydl.dao.GoodsDao;
import com.ydl.dao.GoodsOrderDao;
import com.ydl.entry.dto.GoodsOrderDto;
import com.ydl.entry.pojo.Goods;
import com.ydl.entry.pojo.GoodsOrder;
import com.ydl.entry.vo.goods.UpdateGoodsParam;
import com.ydl.entry.vo.order.AddOrderParam;
import com.ydl.entry.vo.order.DeleteOrderParam;
import com.ydl.entry.vo.order.QueryOrderParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class GoodsOrderService {

    private final GoodsOrderDao goodsOrderDao;


    public ResultVo list(QueryOrderParam queryOrderParam) {
        List<GoodsOrder> goodsOrders = goodsOrderDao.selectList(new LambdaQueryWrapper<GoodsOrder>().like(queryOrderParam.getPdId() != null, GoodsOrder::getPdId, queryOrderParam.getPdId()).eq(queryOrderParam.getGoodsNum() != null, GoodsOrder::getGoodsNum, queryOrderParam.getGoodsNum()));

        return ResultVo.success("查询成功", goodsOrders);

    }

    public ResultVo add(AddOrderParam addOrderParam) {
        GoodsOrder goodsOrder = new GoodsOrder();
        BeanUtils.copyProperties(addOrderParam,goodsOrder);

        String s = IdUtil.randomUUID();
        goodsOrder.setPdId(s);

        int insert = goodsOrderDao.insert(goodsOrder);

        return ResultVo.success();

    }

    public ResultVo update(UpdateGoodsParam updateGoodsParam) {
        GoodsOrder goodsOrder = new GoodsOrder();
        BeanUtils.copyProperties(updateGoodsParam,goodsOrder);

        int i = goodsOrderDao.updateById(goodsOrder);

        return ResultVo.success();
    }

    public ResultVo delete(DeleteOrderParam deleteOrderParam) {

        int i = goodsOrderDao.deleteById(deleteOrderParam.getPdId());
        return ResultVo.success();

    }
}
